package com.wn.common.task;

import com.wn.common.RedisConst;
import com.wn.model.entity.BookType;
import com.wn.service.BookService;
import com.wn.service.BookTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
@EnableScheduling
public class MyTask {
    @Autowired
    BookService bookService;
    @Autowired
    BookTypeService bookTypeService;
    @Autowired
    RedisTemplate redisTemplate;

    @Scheduled(cron = "0/10 * * * 1 ?")
    public void updateHotSelling(){
        redisTemplate.delete(redisTemplate.keys(RedisConst.BOOK_TYPE_LIST+"*"));
        List<BookType> types = bookTypeService.findAll();
        redisTemplate.delete(redisTemplate.keys(RedisConst.BOOK_LIST+"*"));
        bookService.findTopSales(1, 12);
        types.forEach(type -> {
            bookService.findByType(1,12, type.getId());
        });
        log.info("redis更新完毕");
    }



}
